/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.saber.certificacao.beansjsf.prova;

import br.com.saber.certificacao.beansjsf.interfacebean.BeanJSFInterface;
import br.com.saber.certificacao.beansjsf.login.LoginManager;
import br.com.saber.certificacao.beansjsf.semescopo.SelectItemManager;
import br.com.saber.certificacao.ejbs.stateless.interfaces.ProvaBeanLocal;
import br.com.saber.certificacao.modelos.Prova;
import br.com.saber.certificacao.observador.ControleObserver;
import br.com.saber.certificacao.utils.UtilMetodos;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.apache.commons.lang.StringUtils;
import org.primefaces.model.chart.PieChartModel;
import org.primefaces.model.chart.CartesianChartModel;  
import org.primefaces.model.chart.ChartSeries;  
/**
 *
 * @author Guilherme
 */
@ManagedBean(name = "provaReportManager")
@ViewScoped
public class ProvaReportManager implements BeanJSFInterface, Observer {

    @EJB
    private ProvaBeanLocal provaBean;
    
    private Prova provaTerminada;
    
    private PieChartModel pieModel;
    private CartesianChartModel categoryModel;
    private int tamanhoDasBarras = 0;
    
    @ManagedProperty("#{loginManager}")
    private LoginManager loginManager;
    
    @ManagedProperty("#{selectItemManager}")
    private SelectItemManager selectItemManager;

    /** Creates a new instance of ProvaReportManager */
    public ProvaReportManager() {
    }

    //====================
    // Iniciadores
    //====================
    @Override
    @PostConstruct
    public void init() {
        ControleObserver.addBeanObserver(loginManager.getUsuario(), this);
        Logger.getLogger(this.getClass().getName()).log(Level.FINEST, "ProvaReportManager.init() executado!");
    }

    @Override
    @PreDestroy
    public void end() {
         ControleObserver.removeBeanObserver(this.loginManager.getUsuario(), this);
        Logger.getLogger(this.getClass().getName()).log(Level.FINEST, "ProvaReportManager.end() executado!");
    }
    //====================
    //Métodos de Negócio
    //====================
      @Override
    public void update(Observable o, Object arg) {
        String[] args = (String[]) arg;
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals(ControleObserver.Eventos.PROVA_TERMINO) || args[i].equals(ControleObserver.Eventos.PROVA_CRIACAO)) {
                this.provaTerminada = null;
            }
        }
    }
    
    public void atualizarPieChart(){
        Map<String, Number> mapPie = this.provaBean.obterObjetivosProQuantidadeDeErros(provaTerminada);
        this.pieModel = new PieChartModel();
        for(String key : mapPie.keySet()){
            pieModel.set(key, mapPie.get(key));
        }
    }
    
    public void atualizarBarrChart(){
        Map<String, Integer[]> mapBarr = this.provaBean.obterObjetivosIncorretosComTotalQuestoesPorObjetivos(provaTerminada);
        categoryModel = new CartesianChartModel(); 
        ChartSeries questoesErradas = new ChartSeries();
        questoesErradas.setLabel(UtilMetodos.getResourceBundle("labelBarrQuestoesErradas", FacesContext.getCurrentInstance()));
        ChartSeries questoesTotal = new ChartSeries();
        questoesTotal.setLabel(UtilMetodos.getResourceBundle("labelBarrQuestoesObjetivo", FacesContext.getCurrentInstance()));
        tamanhoDasBarras = 0;
        for(String key : mapBarr.keySet()){
            Integer[] i = mapBarr.get(key);
            questoesErradas.set(StringUtils.substring(key,0,15), i[0]);
            questoesTotal.set(StringUtils.substring(key,0,15), i[1]);
            if(tamanhoDasBarras <= i[1] ){
                tamanhoDasBarras = i[1] + 1;
            }
        }
        categoryModel.addSeries(questoesTotal);
        categoryModel.addSeries(questoesErradas);
    }
    
    //====================
    //Table Actions
    //====================
    //====================  
    //SelectItem
    //====================
    //=========================
    //Getters AND Setters
    //=========================

    public LoginManager getLoginManager() {
        return loginManager;
    }

    public void setLoginManager(LoginManager loginManager) {
        this.loginManager = loginManager;
    }

    public Prova getProvaTerminada() {
        return provaTerminada;
    }

    public void setProvaTerminada(Prova provaTerminada) {
        this.provaTerminada = provaTerminada;
        this.atualizarPieChart();//MÉTODO DE NEGÓCIO
        this.atualizarBarrChart();//MÉTODO DE NEGÓCIO
    }

    public SelectItemManager getSelectItemManager() {
        return selectItemManager;
    }

    public void setSelectItemManager(SelectItemManager selectItemManager) {
        this.selectItemManager = selectItemManager;
    }

    public PieChartModel getPieModel() {
        return pieModel;
    }

    public void setPieModel(PieChartModel pieModel) {
        this.pieModel = pieModel;
    }

    public CartesianChartModel getCategoryModel() {
        return categoryModel;
    }

    public void setCategoryModel(CartesianChartModel categoryModel) {
        this.categoryModel = categoryModel;
    }

    public int getTamanhoDasBarras() {
        return tamanhoDasBarras;
    }
}
